package LinkedList.回文链表;

import LinkedList.ListNode;

public class Test {

    public ListNode reverse(ListNode head) {
        ListNode prev = null;
        while(head != null) {
            ListNode next = head.next;
            head.next = prev;
            prev = head;
            head = next;
        }
        return prev;
    }

    public boolean isPalindrome(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode newNode = reverse(slow);
        while(head != null && newNode != null) {
            if(head.val == newNode.val) {
                head = head.next;
                newNode = newNode.next;
            }else {
                return false;
            }
        }
        return true;
    }

}
